package dao;

import bean.Config;
import util.EmailUtils;
import util.SMSUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 其他的dao，例如验证码短信发送、邮件验证发送等
 *
 * @author 邓晴予
 */
public class OtherDao {
    /**
     * The Config dao.
     */
    private ConfigDao configDao = new ConfigDao();

    /**
     * 发送短信验证码
     *
     * @param phone    发送的手机号
     * @param request  the request
     * @param response the response
     * @return boolean
     * @throws Exception the exception
     * @author 邓晴予
     */
    public boolean sendMessage(String phone, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Config url = configDao.getConfig("sms_url");
        Config username = configDao.getConfig("sms_username");
        Config appKey = configDao.getConfig("sms_app_key");
        Config templateId = configDao.getConfig("sms_code_template");

        SMSUtils.sendSMSCode(request, response, url.getValue(), username.getValue(), appKey.getValue(), templateId.getValue(), phone);
        return true;
    }

    /**
     * Send email boolean.
     *
     * @param email    the email
     * @param request  the request
     * @param response the response
     * @return the boolean
     * @throws Exception the exception
     */
    public boolean sendEmail(String email, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Config smtpHost = configDao.getConfig("email_stmp_host");
        Config username = configDao.getConfig("email_username");
        Config password = configDao.getConfig("email_password");
        Config from = configDao.getConfig("email_from");
        EmailUtils.sendEmailCode(request, response, smtpHost.getValue(), username.getValue(), password.getValue(), from.getValue(), email);
        return true;
    }
}
